import axios from 'axios'
const LOGIN = 'LOGIN'
const LOGOUT = 'LOGOUT'
const USER_DATA = 'USER_DATA'

let initState = {
    isAuth: false,
    user: {
        username: 'zhangsan',
        password: '1234'
    }
}

export function auth(state={ ...initState }, action){
    switch(action.type){
        case LOGIN:
            return { ...state, isAuth: true }
        case LOGOUT:
            return { ...state, isAuth: false }
        case USER_DATA:
            return { ...state, user: { username: action.payload.username, password: action.payload.password }}
        default:
            return state
    }
}

export function getUserData(){
    return dispatch => {
        axios.get('/find')
            .then(res => {
                dispatch({ type: USER_DATA, payload: res.data[0] })
            })
    }
}

export function login(){
    return { type: LOGIN }
}
export function logout(){
    return { type: LOGOUT }
}